import { useGlobalStore } from '@/store/global'
import axios from 'axios'

axios.defaults.baseURL = '/api'

// 请求拦截器
axios.interceptors.request.use(
  function (config) {
    if (config.url !== '/login') {
      const { token } = useGlobalStore.getState()
      config.headers.Authorization = token
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

// 响应拦截器
axios.interceptors.response.use(
  function (response) {
    return response.data
  },
  function (error) {
    return Promise.reject(error)
  }
)

type GetData = (url: string, data: any) => Promise<any>

export const GetData: GetData = (url, data) => {
  return axios.get(url, data)
}

type PostData = (url: string, data: any) => Promise<any>

export const PostData: PostData = (url, data) => {
  return axios.post(url, data)
}
